import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:material_collection_app/utils/dio_utils.dart';
import 'package:material_collection_app/utils/utils.dart';
import 'package:material_collection_app/widget/common/top_bar.dart';

class EnterDetailPage extends StatefulWidget  {
  final Map arguments;
  EnterDetailPage({Key key, this.arguments}) : super(key: key);

  @override
  State<StatefulWidget> createState() => MyState();
}

class MyState extends State<EnterDetailPage> {
  Map detail;
  
  @override
  void initState() {
    super.initState();
    setState(() {
      detail = widget.arguments['item'];
    });
  }

  Widget _buildCardHead(title) {
    return Container(
      width: 680.w,
      height: 90.w,
      alignment: Alignment.centerLeft,
      decoration: BoxDecoration(
        border: Border(bottom:BorderSide(width: 1,color: Color(0xFFF1F1F1)))
      ),
      child: Text(title,
        style: TextStyle(
          color: Color(0xFF333333), fontSize: 16, fontWeight: FontWeight.bold
        ),
      ),
    );
  }


  Widget _buildRowText(title, text, [Color color]) {
    return Padding(
      padding: EdgeInsets.fromLTRB(0, 15.w, 0, 15.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text(title.toString(), style: TextStyle(
            color: Color(0xFF999999),
            fontSize: 14
          )),
          Text(text.toString(), style: TextStyle(
            color: color ?? Color(0xFF333333),
            fontSize: 14
          )),
        ],
      ),
    );
  }

  Widget _buildRowPicture() {
    List<Widget> images = [];
    List<String> imgList = [];

    for(var i = 0; i<detail['goods_imgs'].length; i++) {
      var item = detail['goods_imgs'][i];

      imgList.add(DioUtils.API_PREFIX+item);

      images.add(
        GestureDetector(
          onTap: () {
            Navigator.pushNamed(
              context, "/photo_view", 
              arguments: {'list': imgList, 'index': i}
            );
          },
          child: Container(
            width: 100.w,
            height: 100.w,
            decoration: BoxDecoration(
              color: Utils.hexToColor('#F1F3F4'),
              borderRadius: BorderRadius.circular(4.w),
            ),
            margin: EdgeInsets.only(right: 10.w),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(4.w),
              child: Image.network(DioUtils.API_PREFIX+item, 
                fit: BoxFit.cover,
              )
            )
          )
        ),
      );
    }

    return Padding(
      padding: EdgeInsets.fromLTRB(0, 15.w, 0, 15.w),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text('图片', style: TextStyle(
            color: Color(0xFF999999),
            fontSize: 14
          )),
          Container(
            width: 500.w,
            child: Wrap(
              spacing: 10.w,
              runSpacing: 25.w,
              alignment: WrapAlignment.end,
              children: images
            )
          )
        ]
      )
    );
  }

  Widget _buildPageOne() {
    return SingleChildScrollView(
      padding: EdgeInsets.fromLTRB(25.w, 0.w, 25.w, 60.w),
      child: Container(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Container(
              width: 700.w,
              margin: EdgeInsets.only(top: 24.w),
              padding: EdgeInsets.only(bottom: 34.w),
              decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadius.circular(20.w),
              ),
              child: Column(
                children: <Widget>[
                  _buildCardHead('基本信息'),
                  Container(
                    padding: EdgeInsets.fromLTRB(24.w, 20.w, 24.w, 0),
                    child: Column(
                      children: <Widget>[
                        // detail['']
                        _buildRowText('物资编号', '--'),
                        _buildRowText('物资名称', detail['name']),
                        _buildRowText('规格/型号', detail['specification']),
                        _buildRowText('物资分类', detail['mtype'] != null ? detail['mtype']['type_name'] : ''),
                        _buildRowText('入库类型', '--'),
                        _buildRowText('计量单位', detail['unit']),
                        _buildRowText('产品估价', '--'),
                        _buildRowText('有效期', '--'),
                        _buildRowText('来源单位', '--'),
                        _buildRowText('所属仓库', detail['store'] != null ? detail['store']['store_name'] : ''),
                        _buildRowText('所属库区', detail['library'] != null ? detail['library']['library_name'] : ''),
                        _buildRowText('所属库位', detail['shelves'] != null ? detail['shelves']['detail_code'] : ''),
                        _buildRowText('入库人', '--'),
                        _buildRowText('入库时间', '--'),
                        _buildRowText('RFID编码', detail['rfid']),
                        _buildRowText('入库工单', '--'),
                        _buildRowPicture(),
                        _buildRowText('备注', detail['remark']),
                      ],
                    ),
                  )
                ],
              ),
            )
          ],
        ),
      )
    );
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context, width: 750, height: 1334, allowFontScaling: false);

    return DefaultTabController(
      length: 3,
      initialIndex: 0,
      child: Scaffold(
        appBar: TopBar(
          title: '物资详情',
          backgroundColor: Utils.hexToColor('#297CFF'),
        ),
        body: _buildPageOne()
      )
    );
  }
}